# Simple MP3 Voiceprint Comparison Test

$baseUrl = "http://localhost:8082/api/voiceprint"
$audioFile = "test_real_mp3.mp3"
$groupId = "test_group_real_mp3"
$testFeatureId = "test_real_mp3_20250912_233114"

Write-Host "=== Simple MP3 Voiceprint Comparison Test ==="
Write-Host "Audio File: $audioFile"
Write-Host "Group ID: $groupId"
Write-Host "Test Feature ID: $testFeatureId"
Write-Host ""

# Check if audio file exists
if (-not (Test-Path $audioFile)) {
    Write-Host "Error: Audio file not found: $audioFile"
    exit 1
}

# Get file size
$fileSize = (Get-Item $audioFile).Length
Write-Host "File size: $fileSize bytes"
Write-Host ""

# Test 1: 1:1 Comparison using curl-like approach
Write-Host "=== Test 1: 1:1 Voiceprint Comparison ==="
Write-Host "Comparing the same audio file against the registered feature..."

try {
    # Use System.Net.Http.HttpClient for better multipart handling
    Add-Type -AssemblyName System.Net.Http
    
    $httpClient = New-Object System.Net.Http.HttpClient
    $content = New-Object System.Net.Http.MultipartFormDataContent
    
    # Add form fields
    $groupIdContent = New-Object System.Net.Http.StringContent($groupId)
    $content.Add($groupIdContent, "groupId")
    
    $featureIdContent = New-Object System.Net.Http.StringContent($testFeatureId)
    $content.Add($featureIdContent, "featureId")
    
    # Add file
    $fileBytes = [System.IO.File]::ReadAllBytes($audioFile)
    $fileContent = New-Object System.Net.Http.ByteArrayContent -ArgumentList @(,$fileBytes)
    $fileContent.Headers.ContentType = [System.Net.Http.Headers.MediaTypeHeaderValue]::Parse("audio/mpeg")
    $content.Add($fileContent, "audioFile", $audioFile)
    
    $compareUrl = "$baseUrl/compare/one-to-one"
    Write-Host "Sending request to: $compareUrl"
    
    $response = $httpClient.PostAsync($compareUrl, $content).Result
    $responseContent = $response.Content.ReadAsStringAsync().Result
    
    Write-Host "Status Code: $($response.StatusCode)"
    Write-Host "Response: $responseContent"
    
    if ($response.IsSuccessStatusCode) {
        Write-Host "1:1 Comparison successful!"
    } else {
        Write-Host "1:1 Comparison failed with status: $($response.StatusCode)"
    }
    
} catch {
    Write-Host "1:1 Comparison failed: $($_.Exception.Message)"
} finally {
    if ($httpClient) {
        $httpClient.Dispose()
    }
    if ($content) {
        $content.Dispose()
    }
}

Write-Host ""
Write-Host "=== Test 2: 1:N Voiceprint Comparison ==="
Write-Host "Searching for the audio file in the group..."

try {
    # Use System.Net.Http.HttpClient for 1:N comparison
    $httpClient2 = New-Object System.Net.Http.HttpClient
    $content2 = New-Object System.Net.Http.MultipartFormDataContent
    
    # Add form fields
    $groupIdContent2 = New-Object System.Net.Http.StringContent($groupId)
    $content2.Add($groupIdContent2, "groupId")
    
    # Add file
    $fileBytes2 = [System.IO.File]::ReadAllBytes($audioFile)
    $fileContent2 = New-Object System.Net.Http.ByteArrayContent -ArgumentList @(,$fileBytes2)
    $fileContent2.Headers.ContentType = [System.Net.Http.Headers.MediaTypeHeaderValue]::Parse("audio/mpeg")
    $content2.Add($fileContent2, "audioFile", $audioFile)
    
    $searchUrl = "$baseUrl/compare/one-to-many"
    Write-Host "Sending request to: $searchUrl"
    
    $response2 = $httpClient2.PostAsync($searchUrl, $content2).Result
    $responseContent2 = $response2.Content.ReadAsStringAsync().Result
    
    Write-Host "Status Code: $($response2.StatusCode)"
    Write-Host "Response: $responseContent2"
    
    if ($response2.IsSuccessStatusCode) {
        Write-Host "1:N Search successful!"
    } else {
        Write-Host "1:N Search failed with status: $($response2.StatusCode)"
    }
    
} catch {
    Write-Host "1:N Search failed: $($_.Exception.Message)"
} finally {
    if ($httpClient2) {
        $httpClient2.Dispose()
    }
    if ($content2) {
        $content2.Dispose()
    }
}

Write-Host ""
Write-Host "=== All Tests Completed ==="